package com.tanhua.server.controller;

/**
 * @Author: cl
 * @Date: 2022/5/4 - 05 - 04
 * @Description:
 */

import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.MovementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 朋友圈动态处理模块
 */
@RestController
@RequestMapping("/movements")
public class MovementController {

    @Autowired
    private MovementService movementService;

    /**
     * 发布新动态
     *
     * @param movement
     * @param imageContent
     * @return
     */
    @PostMapping
    public ResponseEntity send(Movement movement,
                               MultipartFile imageContent[]) throws IOException {

        movementService.send(movement, imageContent);
        return ResponseEntity.ok("动态发布成功");

    }

    /**
     * 分页查询个人动态(或者好友的可见动态  或者推荐人的所有动态)
     *
     * @param page
     * @param pageSize
     * @param userId
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity getByUserId(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize, Long userId) {

        PageResult pr = movementService.getByUserId(page, pageSize, userId);
        return ResponseEntity.ok(pr);
    }

    /**
     * 获取好友动态
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping()
    public ResponseEntity getFriend(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        PageResult pr = movementService.getFriend(page, pageSize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 推荐列表动态展示
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pageSize) {

        PageResult pr = movementService.recommend(page, pageSize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 通过动态id查询动态
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity getByMovementId(@PathVariable String id) {

        MovementsVo movementsVo = movementService.getByMovementId(id);
        return ResponseEntity.ok(movementsVo);
    }

    /**
     * 点赞
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId) {

        Integer likeCount = movementService.like(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消点赞
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity disLike(@PathVariable("id") String movementId) {

        Integer likeCount = movementService.disLike(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 喜欢
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId) {

        Integer likeCount = movementService.love(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消喜欢
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity disLove(@PathVariable("id") String movementId) {

        Integer likeCount = movementService.disLove(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 最新访客(展示最新访客5条)
     *
     * @return
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors() {
        List<VisitorsVo> vos = movementService.visitors();
        return ResponseEntity.ok(vos);
    }
}
